package cn.hm.file.controller;

import cn.hm.file.test.FastDFSFile;
import cn.hm.file.utils.FastDFSClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/8/11 18:43
 */
@RestController
@CrossOrigin
//@RequestMapping("/file")
@Slf4j
public class FileController {
    /***
     * 文件上传
     * @return
     */
    @PostMapping(value = "/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws Exception {
        //封装一个FastDFSFile
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(), //文件名字
                file.getBytes(),            //文件字节数组
                StringUtils.getFilenameExtension(file.getOriginalFilename()));//文件扩展名

        //文件上传
        String[] uploads = FastDFSClient.upload(fastDFSFile);
        //组装文件上传地址
        log.info("=======图片上传成功，返回图片地址=======");
        return FastDFSClient.getTrackerUrl() + "/" + uploads[0] + "/" + uploads[1];
    }


}
